package com.example.demo.controller;

import com.example.demo.utils.DimensionalUitl;
import com.example.demo.utils.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

/**
 * Created by zouwenchang on 2018/3/7.
 */
@Controller
public class PicturePreviewController {

    @RequestMapping(path="/getImage", method = {RequestMethod.GET})
    public void Imagepeview(@RequestParam("file") String file,HttpServletRequest request, HttpServletResponse response) throws IOException {

       String imagePath = FileUtil.getURL(file);

        URL qrCodePathUrl = new URL(imagePath);

        byte[] buffer = DimensionalUitl.createQRCodeURL(qrCodePathUrl);

        response.setContentType("image/jpeg");// 设置相应信息的类型
        OutputStream os = response.getOutputStream();// 获得servlet的servletoutputstream对象
        os.write(buffer, 0, buffer.length);
        os.flush();
        os.close();
    }
}
